<template>
  <div v-if="isEditor" class="WithNotes" v-loading="loadingInstance" element-loading-text="加载中……">
    <header>
      <el-button type="primary" style=" margin-right: 16px;"><span class="newIcon" @click="newNote">新建笔记</span>
      </el-button>
      <!--      <span class="UploadIcon"></span>-->
      <span class="SettingIcon"></span>
    </header>
    <h4><span class="bijiIcon"></span><span>我的笔记</span></h4>
    <p class="message">碎片信息及时记录，笔记内容将作为个人知识库由AI汇总和分析，提升查找效率并简化文件管理流程。</p>
    <div class="empty" v-if="false">
      <el-empty image="/src/icons/笔记本.svg" :image-size="80" description="暂无笔记" />
    </div>
    <div class="cardList" v-else>
      <el-card v-for="item, index in data" class="cardItem" :key="item.path">
        <div class="header"><span class="time">{{ moment(item.modified).format('YYYY-MM-DD HH:mm') }}</span><span
            class="yunIcon"></span>

          <el-dropdown trigger="click" class="el-dropdown">
            <span class="moreIcon">
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item :icon="EditPen" @click.native="edit(item.path)">编辑笔记</el-dropdown-item>
                <el-dropdown-item :icon="DeleteFilled" @click.native="remove(item.path)">删除此项</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div class="content">
          <div class="title">{{ item.name.split('.').slice(0, -1).join('.') }}</div>
        </div>
      </el-card>
    </div>
  </div>
  <div v-else>
    <editor :message="isEdit" @back="back" :path="path" />
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { getNoteList, removeNote, downLoadNote } from "@/api/index.js";
import editor from "@/views/WithNotes/editor.vue";

import { useEditor } from '@/stores/index'
import moment from "moment";
import { storeToRefs } from 'pinia'
const stro = useEditor()
const { isEditor } = storeToRefs(stro)
const { setEditor } = stro
const isEdit = ref(false)
const path = ref("")

import { ElMessage } from 'element-plus'
import {
  DeleteFilled,
  EditPen,
} from '@element-plus/icons-vue'

const loadingInstance = ref(false)
const data = ref([])


onMounted(async () => {
  setEditor(true)
  initData()
})

const back = () =>{
  isEdit.value = false
  setEditor(true)
  initData()
}

const initData = async () => {
  const res = await getNoteList();
  data.value = res.data.content;
  data.value.sort((a, b) => new Date(b.modified).getTime() - new
    Date(a.modified).getTime())
}

const newNote = () => {
  setEditor(false)
}

const edit = (item) => {
  isEdit.value = true
  path.value = item
  setEditor(false)
}


const remove = async (item) => {
  loadingInstance.value = true
  var path = []
  path[0] = item
  const { success, data } = await removeNote(path)
  if (success == 200) {
    initData()
    loadingInstance.value = false
    ElMessage({ message: '删除成功', type: 'success', })
  }
}


</script>

<style lang="scss" scoped>
.WithNotes {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: start !important;
  flex-direction: column;

  header {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: end;
    margin-top: 16px;

    .newIcon {
      display: flex;
      align-items: center;
      height: 16px;
      padding-left: 20px;
      background: url('@/icons/新建.svg') no-repeat left;
    }

    .UploadIcon {
      display: flex;
      width: 30px;
      height: 20px;
      background: url('@/icons/上传.svg') no-repeat left;
      margin-right: 16px;


    }

    .SettingIcon {
      display: flex;
      width: 20px;
      height: 20px;
      background: url('http://igrsbj.oss-cn-beijing.aliyuncs.com/jar/CloudBoard_xz/icons/setting.svg') no-repeat center;
      margin-right: 16px;


    }

  }

  h4 {
    display: flex;
    align-items: center;
    width: 100%;
    justify-content: center;
    margin-top: 20px;

    span {
      &:nth-child(1) {
        display: flex;
        width: 20px;
        height: 20px;
        background: url('@/icons/随笔记icon.svg') no-repeat center;
        margin-right: 5px;
      }
    }
  }

  .message {
    width: 100%;
    text-align: center;
    font-size: 14px;
    color: #333;
    margin-top: 10px;
  }

  .empty {
    width: 100%;
    margin-top: 150px;
  }

  .cardList {
    overflow: hidden;
    overflow-y: auto;
    width: 100%;
    margin-top: 20px;
    padding: 0 100px;

    .cardItem {

      height: 100px;
      margin: 8px 0;

      .header {
        width: 100%;
        display: flex;
        margin-bottom: 6px;

        .time {
          display: flex;
          align-items: center;
          color: #666;
          text-align: center;
          font-size: 12px;
          font-weight: 600;
        }

        .yunIcon {
          display: flex;
          width: 20px;
          height: 20px;
          background: url('@/icons/云.svg') no-repeat center;
          margin-left: 10px;
        }

        .el-dropdown {
          margin-left: auto;

          .moreIcon {
            display: flex;
            width: 20px;
            height: 20px;
            margin-left: auto;
            background: url('@/icons/更多.svg') no-repeat center;
            background-size: 20px;
          }
        }
      }

      .content {
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        /* 限制在两行内 */
        overflow: hidden;
        text-overflow: ellipsis;
        /* 溢出部分显示省略号 */
      }
    }
  }
}
</style>
